//
//  XRVideoTool.h
//  XR
//
//  Created by xiuren on 2017/3/21.
//  Copyright © 2017年 pepsikirk. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>

//转换/压缩完成的回调
typedef void(^TransformOK)(NSString *mp4Path,NSError* error);


/**
 视频信息model
 */
@interface XRVideoInfoModel : NSObject

//默认第一帧图片
@property (nonatomic,strong) UIImage *previewImage;
//时长单位：秒
@property (nonatomic,assign) NSUInteger duration;
//大小
@property (nonatomic,assign) NSUInteger fileSize;
//分辨率-宽
@property (nonatomic,assign) float width;
//分辨率-高
@property (nonatomic,assign) float height;
//帧率
@property (nonatomic,assign) float fps;
//比特率
@property (nonatomic,assign) float bps;
//旋转角度
@property (nonatomic,assign) CGAffineTransform transform;


@end


@protocol XSJVideoToolDelegate <NSObject>

/**
 解压或者压缩完成/失败时候的回调
 
 @param images   帧图片数组
 @param error    如果成功，error为nil，否则为空
 */
- (void)transferDidFinishedWithImages:(NSMutableArray<UIImage *> *)images error:(NSError *)error;

/**
 保存图片完成的回调

 @param error <#error description#>
 */
- (void)SaveImageCompleted:(NSError *)error;

/**
 保存视频完成的回调
 
 @param error <#error description#>
 */
- (void)SaveVideoCompleted:(NSError *)error;

@end

/**
 视频工具类
 */
@interface XSJVideoTool : NSObject

/**
 将视频解压为图片序列帧

 @param movie 视频文件全路径，确保文件存在
 */
- (void)transferMovie:(NSString *)movie;

@property (nonatomic, weak) id<XSJVideoToolDelegate> delegate;

/**
 *  将视频url转化为NSData数据,写入沙盒
 *
 *  @param 视频url   图片
 *  @param filePath   文件路径
 *
 *  @return
 */
+ (void)videoWithUrl:(NSURL *)url FilePath:(NSString *)filePath;


/**
 得到视频相关信息

 @param videoUrl 视频地址
 @return <#return value description#>
 */
+(XRVideoInfoModel*)getVideoInfo:(NSURL*)videoUrl;

//获取视频的大小
+ (NSUInteger)getVideoFileSize:(NSString *)videoUrlString;

//获取视频的大小
+ (NSUInteger)getVideoFileSizebyAsset:(PHAsset *)asset;


/**
 获取视频的任意一帧

 @param videoUrl 视频地址
 @param fTime 时间点
 @return 返回图片
 */
+ (UIImage *)getVideoPreViewImage:(NSURL *)videoUrl time:(NSInteger)ftime;

/**
 将phasset 压缩 获取url

 @param asset PHAsset
 @param success <#success description#>
 @param failure <#failure description#>
 @param cancel <#cancel description#>
 */
+ (void)compressWithAsset:(PHAsset *)asset success:(void(^)(NSString *url, NSString *scope))success failure:(void(^)(NSString *errorString))failure cancel:(void(^)())cancel;

/**
 删除已压缩的视频

 @param scope 文件名
 */
+ (void)removeMovieWithScope:(NSString *)scope;

/**
 获取传入时间节点的帧图片（可控制是否为关键帧）

 @param outMovieURL <#outMovieURL description#>
 @param time <#time description#>
 @param isKeyImage <#isKeyImage description#>
 @return <#return value description#>
 */
+ (UIImage *)getCoverImage:(NSURL *)outMovieURL atTime:(CGFloat)time isKeyImage:(BOOL)isKeyImage;

/**
 获取多媒体时长

 @param mediaUrlStr <#mediaUrlStr description#>
 @return <#return value description#>
 */
+ (CGFloat)getMediaDurationWithMediaUrl:(NSString *)mediaUrlStr;

/**
 mov转mp4

 @param videoUrl <#videoUrl description#>
 @param completion <#completion description#>
 */
+(void)movTomp4:(NSURL*)videoUrl completion:(TransformOK)completion;


/**
 视频压缩

 @param videoUrl <#videoUrl description#>
 @param compressionType
 AVAssetExportPresetLowQuality
 AVAssetExportPresetMediumQuality
 VAssetExportPresetHighestQuality
 AVAssetExportPreset640x480
 AVAssetExportPreset1280x720
 @param completion <#completion description#>
 */
+(void)videoCompress:(NSURL*)videoUrl compressionType:(NSString *)compressionType completion:(TransformOK)completion;

/**
 保存图片到相册

 @param image <#image description#>
 @param delegate <#delegate description#>
 */
-(void)SaveImageToAlbum:(UIImage*)image withDelegate:(id <XSJVideoToolDelegate>)delegate;

/**
 保存视频到相册

 @param videoPath <#videoPath description#>
 @param delegate <#delegate description#>
 */
-(void)SaveVideoToAlbum:(NSString*)videoPath withDelegate:(id <XSJVideoToolDelegate>)delegate;


/**
 将视频数据转为图片

 @param sampleBuffer
 @return
 */
+(UIImage*)CMSampleBufferRefToUIImage:(nonnull CMSampleBufferRef)sampleBuffer;


/**
 将图片转为视频数据源

 @param image
 @return 
 */
+(CVPixelBufferRef _Nullable )UIImageToCVPixelBufferRef:(nonnull UIImage*)image;


@end
